/*******************************************************************************
 * Copyright (c) 2007 University of Oslo.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     University of Oslo - initial API and implementation
 *******************************************************************************/

package com.cea.papyrus.diagram.sequence.sedi.model.command;

import org.eclipse.gef.commands.Command;
import org.eclipse.uml2.uml.NamedElement;

/**
 * Command to rename NamedElements.
 * 
 * @author Andreas Limyr
 * @author frankd@ifi.uio.no
 */
public class DirectEditCommand extends Command {
	private NamedElement model;
	private String newName;
	private String oldName;

	public DirectEditCommand() {
	}

	public DirectEditCommand(final NamedElement element) {
		this(element, null);
	}

	public DirectEditCommand(final NamedElement element, final String newName) {
		setNamedElement(element);
		setName(newName);
	}

	public void setNamedElement(final NamedElement element) {
		this.model = element;
	}

	public void setName(final String name) {
		this.newName = name;
	}

	@Override
	public void execute() {
		oldName = model.getName();
		model.setName(newName);
	}

	@Override
	public void undo() {
		model.setName(oldName);
		oldName = null;
	}

	@Override
	public void redo() {
		oldName = model.getName();
		model.setName(newName);
	}

	@Override
	public void dispose() {
		model = null;
		oldName = null;
		newName = null;
	}
}
